package net.blf2.config;

import net.blf2.cache.MainCache;

public class SimpleCacheBuilder {

    //是否开启自动回收
    private boolean isAutoCollect = false;
    //自动回收间隔时间 只有isAutoCollect有值才会生效 单位：秒
    private long autoCollectInterval = 10000L;
    //默认存活时间 单位：秒
    private long defaultTimeToLive = 10000L;

    private static SimpleCacheBuilder simpleCacheBuilder = SimpleCacheBuilderInstance.INSTANCE;

    private static final MainCache mainCache = new MainCache();

    public static MainCache build(){
        return mainCache;
    }

    public static MainCache build(SimpleCacheConfig config){

        simpleCacheBuilder.setAutoCollect(config.isAutoCollect())
                .setAutoCollectInterval(config.getAutoCollectInterval())
                .setDefaultTimeToLive(config.getDefaultTimeToLive());
        return mainCache;
    }

    private SimpleCacheBuilder(){}

    private static class SimpleCacheBuilderInstance{
        private static SimpleCacheBuilder INSTANCE = new SimpleCacheBuilder();
    }

    public static boolean  isAutoCollect() {
        return simpleCacheBuilder.isAutoCollect;
    }

    public static SimpleCacheBuilder setAutoCollect(boolean autoCollect) {
        simpleCacheBuilder.isAutoCollect = autoCollect;
        return simpleCacheBuilder;
    }

    public static long getAutoCollectInterval() {
        return simpleCacheBuilder.autoCollectInterval;
    }

    public static SimpleCacheBuilder setAutoCollectInterval(long autoCollectInterval) {
        simpleCacheBuilder.autoCollectInterval = autoCollectInterval;
        return simpleCacheBuilder;
    }

    public static long getDefaultTimeToLive() {
        return simpleCacheBuilder.defaultTimeToLive;
    }

    public static SimpleCacheBuilder setDefaultTimeToLive(long defaultTimeToLive) {
        simpleCacheBuilder.defaultTimeToLive = defaultTimeToLive;
        return simpleCacheBuilder;
    }

    public static MainCache getMainCache() {
        return mainCache;
    }

}
